package util;

import java.awt.Color;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;

import org.apache.ibatis.session.SqlSession;

import baseDao.LogDao;
import entity.Log;
import swing.Index;

public class LogUtil {
	public final static Integer RED=3;
	public final static Integer BLACK=1;
	public final static Integer GREEN=2;
	public static void logInsert(Integer color,String name){
		StyledDocument d=Index.textArea.getStyledDocument();  
        SimpleAttributeSet attr = new SimpleAttributeSet();  
        Log l=new Log();
        Date date= new Date();
        l.setDate(String.valueOf(date.getTime()));
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if(color.equals(3)){
        	l.setStatus(3);
        	StyleConstants.setForeground(attr, Color.red);  
        }else if(color.equals(1)){
        	l.setStatus(1);
        	StyleConstants.setForeground(attr, Color.black);  
        }else if(color.equals(2)){
        	l.setStatus(2);
        	StyleConstants.setForeground(attr, Color.green);
        }
        l.setName(name);
        SqlSession session = GetSqlSession.getSession();
        LogDao logdao = session.getMapper(LogDao.class);
        logdao.insertLog(l);
        GetSqlSession.closeSession(session);
        try {
			d.insertString(d.getLength(),sdf.format(date)+"\t"+name+"\n\n",attr);
		} catch (BadLocationException e1) {
			e1.printStackTrace();
		}  
	}
}
